package com.atguigu.admin.service.impl;

import com.atguigu.admin.mapper.SysRoleMapper;
import com.atguigu.admin.model.entity.SysRole;
import com.atguigu.admin.model.entity.SysUserRole;
import com.atguigu.admin.model.vo.AssginRoleVo;
import com.atguigu.admin.model.vo.SysRoleQueryVo;
import com.atguigu.admin.service.SysRoleService;
import com.atguigu.admin.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-10-28
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public IPage<SysRole> selectPage(Page<SysRole> pageParam, SysRoleQueryVo roleQueryVo) {
        //try {
        //    int a = 10 / 0;
        //} catch (Exception e) {
        //    throw new GuiguException(20001, "出现自定义异常");
        //}
        return baseMapper.selectPage(pageParam, roleQueryVo);
    }

    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //获取所有角色
        List<SysRole> roles = baseMapper.selectList(null);
        //根据用户id查询
        List<SysUserRole> userRoles = sysUserRoleService.getSysUserRoles(userId);
        //获取用户已分配的角色id
        List<Long> userRoleIds = new ArrayList<>();
        for (SysUserRole userRole : userRoles) {
            userRoleIds.add(userRole.getRoleId());
        }
        //创建返回的Map
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles", roles);
        returnMap.put("userRoleIds", userRoleIds);
        return returnMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAssign(AssginRoleVo assginRoleVo) {
        //根据用户id删除原来分配的角色
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", assginRoleVo.getUserId());
        sysUserRoleService.remove(queryWrapper);
        //获取所有的角色id
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        List<SysUserRole> insertList = new ArrayList<>();
        for (Long roleId : roleIdList) {
            if (roleId != null) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(assginRoleVo.getUserId());
                sysUserRole.setRoleId(roleId);
                insertList.add(sysUserRole);
            }
        }
        sysUserRoleService.saveBatch(insertList);
    }

    @Override
    public List<String> getUserRoleNamesByUserId(Long userId) {
        return baseMapper.selectUserRoleNamesByUserId(userId);
    }

}
